#include "common.h"
namespace common {
bool containsNearbyDuplicate(std::vector<int> &nums, int k) {
    std::unordered_map<int, int> map;
    bool result = false;
    for (int i = 0; i < nums.size(); i++) {
        if (map.count(nums[i])) {
            int loc = map[nums[i]];
            if (std::abs(i - loc) <= k) {
                result = true;
                break;
            } else {
                map[nums[i]] = i;
            }
        } else {
            map[nums[i]] = i;
        }
    }
    return result;
}
} // namespace common
